<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        ul,
        li {
            list-style: none;
        }

        #form {
            width: 600px;
            height: 40px;
            border-radius: 10px;
            border: 2px solid #ccc;
            margin: auto;
            margin-top: 100px;
            position: relative;
        }

        #form.active {
            border: none;
        }

        #form #keyWord {
            position: absolute;
            width: 94%;
            height: 100%;
            top: 0px;
            left: 0px;
            z-index: 1;
            border: none;
            outline: none;
            right: 0px;
            bottom: 0px;
            margin: auto;
            background: rgba(0, 0, 0, 0);
            /* border-bottom: 2px solid rgba(0, 0, 0, 0.5); */
        }

        #form .list {
            position: absolute;
            top: -2px;
            left: -2px;
            width: 100%;
            height: auto;
            border-radius: 10px;
            padding-top: 45px;
            border: 2px solid blue;
            display: none;
        }

        #form .list li {
            height: 30px;
            color: #626675;
        }

        b {
            font-weight: 400;
            color: #222;
        }
    </style>
</head>

<body>
    <form id="form" class="">
        <input type="text" id="keyWord" />
        <div class="list">
            <ul>
                <!-- <li data-key="中通快递" class="bdsug-overflow">中<b>通快递</b></li> -->
            </ul>
        </div>
    </form>
    <script>
        // 获取元素
        let inp = document.querySelector('#keyWord'); //输入框
        let list = document.querySelector('.list');
        let UL = list.querySelector('ul'); //ul
        // 绑定输入框事件
        inp.oninput = () => {
            list.style.display = 'block';
            // 创建script元素
            let script = document.createElement("script");
            script.src =
                `https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=34379,34554,34004,34585,34092,26350,34556&wd=${inp.value}&req=2&csor=1&cb=fun`;
            // 把script放到body里面
            document.body.appendChild(script);
            // 删除script
            script.remove();
        }
        // 封装一个函数获取数据
        function fun(res) {
            // for (let key in res) {
            //     console.log(key);
            // }
            let date = res.g;
            neirong(UL, date);
        }
        // 封装渲染函数
        function neirong(UL, date) {
            UL.innerHTML = date.map(item => {
                return `<li class="bdsug-overflow">
                                ${item.q}
                        </li>`
            }).join('');
        }
    </script>
</body>

</html>